import socket

# 创建TCP socket对象
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定IP地址和端口号
server_socket.bind(('localhost', 8000))

# 开始监听客户端连接请求
server_socket.listen(1)
print('Server started, listening on port 8000...')

while True:
    # 接受客户端连接请求
    conn, addr = server_socket.accept()
    print('Client connected from', addr)

    # 接收客户端数据
    data = conn.recv(1024)
    print('Received message:', data.decode())

    # 发送响应数据给客户端
    conn.sendall('Hello, client!'.encode())

    # 关闭连接
    conn.close()
